"""
【交给中介】
中介者模式(Mediator Pattern)
定义了一个中介对象来封装一系列对象之间的交互。这个模式使得各对象之间不需要显式地相互引用，从而使其耦合度降低，同时也有助于复杂系统的维护。
"""
from abc import ABC, abstractmethod


class Colleague(ABC):
    def __init__(self, mediator):
        self.mediator = mediator

    @abstractmethod
    def send(self, message):
        pass

    @abstractmethod
    def receive(self, message):
        pass


class ConcreteColleagueA(Colleague):
    def send(self, message):
        self.mediator.send_message(message, self)

    def receive(self, message):
        print(f"ColleagueA received message:{message}")


class ConcreteColleagueB(Colleague):
    def send(self, message):
        self.mediator.send_message(message, self)

    def receive(self, message):
        print(f"ColleagueB received message:{message}")


class Mediator:
    def __init__(self):
        self.colleagues = []

    def add_colleague(self, colleague):
        self.colleagues.append(colleague)

    def send_message(self, message, sender):
        for colleague in self.colleagues:
            if colleague != sender:
                colleague.receive(message)


mediator = Mediator()
# 创建
colleague_a = ConcreteColleagueA(mediator)
colleague_b = ConcreteColleagueB(mediator)
# 注册
mediator.add_colleague(colleague_a)
mediator.add_colleague(colleague_b)
# 通过中介对象
colleague_a.send("Hello, colleague B")
colleague_b.send("Hi, colleague A")
"""
ColleagueB received message: Hello, colleague B
ColleagueA received message: Hi, colleague A
"""
